﻿package com.tudou.v2
{
    import com.tudou.consts.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;

    public class VideoInfo extends Object implements IVideoInfo
    {
        protected var v2StrXML:String;
        protected var _iid:int = 0;
        private var _code:String;
        private var _isAudio:Boolean = false;
        protected var _v2ErrorNo:int = 0;
        var v2URL:String = "";
        private var _logoMaskList:Array;
        private var _videoTotalTime:Number = 0;
        protected var sourceId:String = "0";
        private var videoTitle:String;
        protected var rateURLSet:Array;
        protected var urlLength:uint = 0;
        private var _channelId:int;
        public static const HD_480:int = 4;
        public static const HD_360:int = 3;
        public static const NORMAL_256_F4V:int = 2;
        public static const NORMAL_256_FLV:int = 1;
        public static const ORIGINAL:int = 99;
        public static const HD_720:int = 5;

        public function VideoInfo(param1:Object, param2:String, param3:int, param4:String) : void
        {
            rateURLSet = [];
            this.sourceId = param2;
            this.v2URL = param4;
            v2StrXML = param1.toString();
            _iid = param3;
            parseBaseInfo(param1);
            return;
        }// end function

        public function getRateTypeURLs() : Array
        {
            return rateURLSet.concat();
        }// end function

        private function getVideoURLs(param1:XML) : void
        {
            var _loc_3:XML = null;
            var _loc_4:String = null;
            var _loc_5:Array = null;
            var _loc_6:VideoNodeInfo = null;
            var _loc_7:int = 0;
            var _loc_2:* = param1.f;
            urlLength = _loc_2.length();
            for each (_loc_3 in _loc_2)
            {
                
                _loc_4 = _loc_3.toString();
                if (!isFlvF4v(_loc_4))
                {
                    continue;
                }
                _loc_7 = getRateType(_loc_3.@st);
                if (rateURLSet[_loc_7] == null)
                {
                    rateURLSet[_loc_7] = [];
                }
                _loc_5 = rateURLSet[_loc_7];
                _loc_6 = new VideoNodeInfo();
                _loc_6.url = VideoURL.addSourceId(_loc_4, sourceId);
                _loc_6.bandCtrl = int(_loc_3.@bc) / 100;
                _loc_6.st = int(_loc_3.@st);
                _loc_5.push(_loc_6);
            }
            return;
        }// end function

        public function get logoMaskList() : Array
        {
            return _logoMaskList;
        }// end function

        public function get isAudio() : Boolean
        {
            return _isAudio;
        }// end function

        public function get title() : String
        {
            return videoTitle;
        }// end function

        public function next(param1:int) : void
        {
            var _loc_2:* = rateURLSet[param1];
            if (_loc_2 && _loc_2.length > 0)
            {
                _loc_2.shift();
            }
            return;
        }// end function

        private function isFlvF4v(param1:String) : Boolean
        {
            var _loc_2:* = VideoType.getType(param1);
            return _loc_2 == VideoType.F4V || _loc_2 == VideoType.FLV || _loc_2 == VideoType.M4V;
        }// end function

        public function getCurrVideoNodeInfo(param1:int) : VideoNodeInfo
        {
            var _loc_2:* = rateURLSet[param1];
            if (_loc_2 && _loc_2.length > 0)
            {
                return _loc_2[0];
            }
            return new VideoNodeInfo();
        }// end function

        public function get v2ErrorNo() : int
        {
            return _v2ErrorNo;
        }// end function

        public function getAllNodeUrl() : Array
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_6:Array = null;
            var _loc_7:String = null;
            var _loc_5:Array = [];
            _loc_2 = rateURLSet.length - 1;
            while (_loc_2 >= 0)
            {
                
                _loc_6 = rateURLSet[_loc_2];
                if (_loc_6 && _loc_6.length > 0)
                {
                    _loc_4 = 0;
                    _loc_3 = _loc_6.length;
                    while (_loc_4 < _loc_3)
                    {
                        
                        _loc_7 = (_loc_6[_loc_4] as VideoNodeInfo).url;
                        if (_loc_7)
                        {
                            _loc_5.push(_loc_7);
                        }
                        _loc_4++;
                    }
                }
                _loc_2 = _loc_2 - 1;
            }
            return _loc_5;
        }// end function

        public function get channelId() : uint
        {
            return _channelId;
        }// end function

        private function parseBaseInfo(param1:Object) : void
        {
            var v2XML:XML;
            var tempLogo:Array;
            var lp:uint;
            var i:int;
            var xml:* = param1;
            try
            {
                v2XML = new XML(xml);
                if (v2XML.@errno.length() > 0)
                {
                    _v2ErrorNo = v2XML.@errno;
                }
                else
                {
                    _logoMaskList = new Array();
                    tempLogo;
                    _isAudio = v2XML.@vi == "0";
                    _channelId = uint(v2XML.@ch);
                    _code = v2XML.@cd;
                    _videoTotalTime = int(v2XML.@tm) / 1000;
                    videoTitle = String(v2XML.@tt);
                    tempLogo.push(uint(v2XML.@lg));
                    tempLogo.push(uint(v2XML.@wt));
                    i;
                    while (i < tempLogo.length)
                    {
                        
                        lp = tempLogo[i];
                        if (lp >= 1 && lp <= 4)
                        {
                            _logoMaskList.push(lp);
                        }
                        i = (i + 1);
                    }
                    getVideoURLs(v2XML);
                }
            }
            catch (error:Error)
            {
                _v2ErrorNo = 2;
                ErrorReportTracker.send(_iid, ErrorReportTracker.CAN_NOT_PLAY, "parseXMLError", ErrorReportTracker.CAN_NOT_PLAY_PARSE_XML_ERROR);
                ErrorTracker.send(sourceId, ErrorTracker.CAN_NOT_PLAY, ErrorTracker.CAN_NOT_PLAY_PARSE_XML_ERROR, v2URL, v2StrXML);
            }
            return;
        }// end function

        public function get code() : String
        {
            return _code;
        }// end function

        public function get videoTotalTime() : Number
        {
            return _videoTotalTime;
        }// end function

        public function get iid() : int
        {
            return _iid;
        }// end function

        static function getRateType(param1:int) : int
        {
            if (param1 == HD_360)
            {
                return RateType.P360;
            }
            if (param1 == HD_480)
            {
                return RateType.P480;
            }
            if (param1 == HD_720)
            {
                return RateType.P720;
            }
            if (param1 == ORIGINAL)
            {
                return RateType.ORIGINAL;
            }
            return RateType.P256;
        }// end function

    }
}
